/**
 * function loading Note Issue Tab Panel by Ajax
 */

var dlg_create = null;
function reloadNoteIssueTabPanel () {
	var issueKey = getIssueKey(),
		$li = AJS.$(AJS.format('#issue-tabs li[data-id="{0}"].active', 'note-tab-panel')),
		request = {
			url: AJS.contextPath() + "/GetNoteIssueTabPanel.jspa",
			data: {},
			type: "GET",
			success: function (data) {
				AJS.$('#issue_actions_container').html(data);
			}
		};
	// call ajax request if exists issueKey and the note-tab-panel is active
	if (issueKey != '' && $li.length > 0) {
		// set request data
		request.data['issueKey'] = issueKey;
		request.data['decorator'] = 'none';

		// call ajax request
		AJS.$.ajax(request);
	}
}

/**
 * function loading latest note by Ajax
 */
function reloadLatestNote () {
	var issueKey = getIssueKey(),
		request = {
			url: AJS.contextPath() + "/GetLatestNote.jspa",
			data: {},
			type: "GET",
			success: function (data) {
				AJS.$('#note-issue-web-panel .mod-content').html(data);
				displayLatestNote();
			}
		};
	// call ajax request if exists issueKey
	if (issueKey != '') {
		// set request data
		request.data['issueKey'] = issueKey;
		request.data['decorator'] = 'none';

		// call ajax request
		AJS.$.ajax(request);
	}
}

/**
 * This function will show or hide the latest note on issue web panel  
 */
function displayLatestNote () {
	// do nothing if not exist the note issue web panel
	if (AJS.$('#note-issue-web-panel').length == 0) {
		return;
	}
	// hide latest note if the web panel is empty
	// or contains "empty-content" class
	if (AJS.$('#note-issue-web-panel .mod-content').html() == ''
		|| AJS.$('#note-issue-web-panel .mod-content .empty-content').length > 0) {
		AJS.$('#note-issue-web-panel').hide();
		// 
		if (AJS.$('#viewissuesidebar .module').length > 1) {
			AJS.$('#note-issue-web-panel').next().addClass('first-module');
		}		
	} else {
		// show latest note
		AJS.$('#note-issue-web-panel').show();
		// remove "first-module" class
		AJS.$('#viewissuesidebar .first-module').removeClass('first-module');
	}	
}

jQuery(document).ready(function() {
	onCreateNoteByAjaxCompleted = function (data) {
		// if action success then reload page, else show the error message
		if (data.success === true) {
			// close form
			dlg_create.hide();
			var issueKey = getIssueKey();
			// reload page if exists a input with name "reloadPageOnActionSuccess"
			if (AJS.$('input[name="reloadPageOnActionSuccess"]').length > 0) {
				reloadPage();
			}
			// reload Note Issue Tab Panel and latest note 
			// if exists issueKey and equals to tagged issue 
			else if (issueKey != '' &&
				issueKey == AJS.$('#note-create-form input[name="issueKey"]').val()
				) {
				reloadLatestNote();
				reloadNoteIssueTabPanel();
			}
		} else {
			// show error message
			AJS.$('.pop-up-disable').remove();
			AJS.$('#note-create-form #noteOperationErrorMessage').html(data.response.errorMessages[0]);
			AJS.$('#note-create-form .error-row').show();
		}
	},

	// This function process the submit create form event 
	onSubmitCreateForm = function () {
		AJS.$('.dialog-components').append('<div class="aui-blanket pop-up-disable"></div>');
		var request = {
			url: AJS.contextPath() + "/CreateNote.jspa",
			data: {},
			dataType: "json",
			type: "GET",
			success: onCreateNoteByAjaxCompleted
		};
		// set request data
		request.data['content'] = fixedEncodeURIComponent(AJS.$('#note-create-form textarea[name="content"]').val());
		request.data['priority'] = AJS.$('#note-create-form input[name="priority"]:checked').val();
		request.data['userName'] = AJS.$('#note-create-form input[name="userName"]').val();
		request.data['issueKey'] = AJS.$('#note-create-form input[name="issueKey"]').val();
		request.data['atl_token'] = AJS.$('#note-create-form input[name="atl_token"]').val();
		request.data['decorator'] = 'none';

		// call ajax request
		AJS.$.ajax(request);
	},

	initDialogCreate = function () { 
		dlg_create = new AJS.Dialog({
			width : 600,
			height : 300,
			id : "note-create-dialog",
			closeOnOutsideClick : false
		});
		dlg_create.addHeader(AJS.I18n.getText("create.note.name"));
		dlg_create.addPanel(AJS.I18n.getText("panel.name"), '');
		dlg_create.addSubmit(AJS.I18n.getText("create.name"), onSubmitCreateForm);
		dlg_create.addSubmit(AJS.I18n.getText("reset.name"), resetCreateFormData);
		dlg_create.addLink(AJS.I18n.getText("cancel.name"), function (dialog) {
		    dialog.hide();
		});
	},
	
	resetCreateFormData = function () {
		var prefix = '#note-create-form ';
		AJS.$('.pop-up-disable').remove();
		AJS.$(prefix + 'input[name="priority"]:first').attr('checked', 'checked');
		AJS.$(prefix + 'input[name="userName"]').val('');
		AJS.$(prefix + 'input[name="issueKey"]').val('');
		AJS.$(prefix + 'textarea[name="content"]').val('');
		AJS.$(prefix + '#noteOperationErrorMessage').html('');
		AJS.$(prefix + '#remain').html('');
	},
	
	showCreateDialog = function () {
		if (!dlg_create) {
			AJS.$('#note-create-form').remove();
			initDialogCreate();
		}
		resetCreateFormData();
		dlg_create.show();
	};
	AJS.$("#create-note-link").click(function (e) {
		e.preventDefault();
		AJS.$.ajax({
			url: AJS.$("#create-note-link").attr('href') + '?decorator=none',
			type: "GET",
			error: function() { },
			success: function(data, textStatus, XMLHttpRequest) {
				AJS.$('#note-create-dialog .dialog-panel-body').html(data);
			}
		});
		showCreateDialog();
		return false;
	});
	
	displayLatestNote();
});